package com.spark.check

import android.content.Intent
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.check.model.ToBeTaskModel
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.toast
import com.spark.common.ext.wrapIO
import com.spark.common.model.ResultModel
import com.spark.common.net.Http
import com.spark.common.widget.Alert
import kotlinx.android.synthetic.main.frag_to_check.*

/**
 * 待检查任务
 */
class ToCheckFragment : BaseFragment() {
    private var id = ""
    private lateinit var checkAdapter: CheckAdapter

    companion object {
        fun newInstance(id: String): ToCheckFragment {
            val fragment = ToCheckFragment()
            val args = Bundle()
            args.putString(Const.qr_taskid, id)
            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_to_check
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        id = arguments?.getString(Const.qr_taskid) ?: ""
        checkAdapter = CheckAdapter(R.layout.item_check)
        recy_checks.adapter = checkAdapter
        recy_checks.layoutManager = LinearLayoutManager(context)

        iv_back.setOnClickListener { activity?.finish() }

        checkAdapter.addData(mutableListOf(
                ToBeTaskModel.Data.Item(),
                ToBeTaskModel.Data.Item(),
                ToBeTaskModel.Data.Item(),
                ToBeTaskModel.Data.Item(),
                ToBeTaskModel.Data.Item(),
                ToBeTaskModel.Data.Item()
        ))
        checkAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = checkAdapter.data[position]

        }

        tv_rebut.setOnClickListener {

        }

        tv_pass.setOnClickListener {
            activity?.apply {
                Alert.Builder(this, "是否提交结果？")
                        .affirm("是") { d, i ->
                            d.dismiss()
                        }
                        .cancel("否") { d, i ->
                            d.dismiss()
                        }
                        .create()
                        .show()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

    }

    /**
     * 获取待检查的任务
     */
    fun fetchToCheckTasks() {
        val params = mutableMapOf(
                Pair(Const.qr_taskid, id)
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_task_check_cooper_list}", params)
                    .wrapIO()
                    .subscribe(
                            {
                                Gson().fromJson(it, ToBeTaskModel::class.java)?.apply {
                                    if (code == "200") {
                                        checkAdapter.addData(data.items)
                                    } else {
                                        toast(msg)
                                    }
                                }
                            },
                            {
                                it.printStackTrace()
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                dispose = it
                                loading()
                            }
                    )
        }
    }

    /**
     * 提交检查结果
     */
    fun submit() {
        val params = mutableMapOf(
                Pair("", "")
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_task_check_submit}", params)
                    .wrapIO()
                    .subscribe(
                            {
                                Gson().fromJson(it, ResultModel::class.java)?.apply {
                                    if (code == "200") {
                                        toast("提交成功")
                                        pop()
                                    } else {
                                        toast(msg)
                                    }
                                }
                            },
                            {
                                it.printStackTrace()
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                loading("提交中...")
                            }
                    )
        }
    }
}

class CheckAdapter(@LayoutRes layoutRes: Int) : BaseQuickAdapter<ToBeTaskModel.Data.Item, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: ToBeTaskModel.Data.Item?) {
        if (item == null) return
        helper?.apply {
            /*setText(R.id.tv_title, "${item.serialno} ${item.categoryname}")
            setText(R.id.tv_category, "${item.categoryname.split("/", "-")}")
            setText(R.id.tv_opinion, item.resultremark)*/

            setText(R.id.tv_title, "01.01.01 专业技能及作业规范")
            setText(R.id.tv_category, "露天煤矿-岗位规范-专业技能及作业规范")
            setText(R.id.tv_opinion, "意见：请注意检查的方式和技巧")
            getView<TextView>(R.id.tv_to_check).setOnClickListener {
                onItemChildClickListener?.onItemChildClick(this@CheckAdapter, it, adapterPosition)
            }


        }
    }


}